#include <gtest/gtest.h>

#include "util/strutil.h"


TEST(strutil, startWith) {
    using strutil::startWith;

    ASSERT_TRUE(startWith("", ""));
    ASSERT_TRUE(startWith("a", ""));
    ASSERT_TRUE(startWith("a", "a"));
    ASSERT_TRUE(startWith("abc", "a"));
    ASSERT_TRUE(startWith("abc", "abc"));
    ASSERT_TRUE(startWith("abc", "ab"));
    ASSERT_FALSE(startWith("", "a"));
    ASSERT_FALSE(startWith("ab", "abc"));
}

TEST(strutil, interpolate) {
    using strutil::interpolate;

    ASSERT_EQ(interpolate({}, ""), "");
    ASSERT_EQ(interpolate({}, "${name}"), "");
    ASSERT_EQ(interpolate({}, "abc${name}"), "abc");
    ASSERT_EQ(interpolate({}, "${name}abc"), "abc");
    ASSERT_EQ(interpolate({}, "${name}", nullptr), "");
    ASSERT_EQ(interpolate({}, "abc${name}", nullptr), "abc");
    ASSERT_EQ(interpolate({}, "abc${name}def"), "abcdef");
    ASSERT_EQ(interpolate({}, "${name}", "default"), "default");
    ASSERT_EQ(interpolate({}, "abc${name}", "default"), "abcdefault");

    ASSERT_EQ(interpolate({}, "abc$def"), "abc$def");
    ASSERT_EQ(interpolate({}, R"(abc\$def)"), "abc$def");

    ASSERT_EQ(interpolate({ { "n1","v1" } }, "abc${n1}def"), "abcv1def");
    ASSERT_EQ(interpolate({ {"n1","v1"},{"n2","v2"} }, "${n1}-${n2}"), "v1-v2");

    ASSERT_EQ(interpolate({ {"n","p2"} }, R"(\\p1\\${n}\\p3)"), R"(\p1\p2\p3)");
    ASSERT_EQ(interpolate({}, R"(\\p1\\p2\\)"), R"(\p1\p2\)");
}

TEST(strutil, removeLineComment) {
    auto f = [](const std::string &s, char sep) {
        std::string result(s);
        strutil::removeLineComment(result, sep);
        return result;
    };

    ASSERT_EQ(f("", '#'), "");
    ASSERT_EQ(f("a", '#'), "a");
    ASSERT_EQ(f("abc", '#'), "abc");

    ASSERT_EQ(f("#", '#'), "");
    ASSERT_EQ(f("#aaa", '#'), "");
    ASSERT_EQ(f("a#b", '#'), "a");
    ASSERT_EQ(f("abc#def", '#'), "abc");
    ASSERT_EQ(f("a#b#c", '#'), "a");
    ASSERT_EQ(f("abc#def#g", '#'), "abc");
}
